//
//  PKCommunityPostTableViewCell.m
//  PianKe
//
//  Created by lanou on 15/12/30.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKCommunityPostTableViewCell.h"

@implementation PKCommunityPostTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self addSubviews];

        
    }

    return  self;
}

- (void)addSubviews
{
    
    _hotImageV = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 30*kWidthBase), -2*kHeightBase, 30*kWidthBase, 30*kWidthBase)];
    _hotImageV.image = nil;
    [self.contentView addSubview:_hotImageV];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*kWidthBase, 20*kHeightBase, kScreenWidth - 40*kWidthBase, 50*kHeightBase)];
    [self.contentView addSubview:_titleLabel];
    
    _coverImageV = [[UIImageView alloc] initWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + 10*kWidthBase, 50*kWidthBase, 50*kWidthBase)];
    [self.contentView addSubview:_coverImageV];
    
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_coverImageV.right + 10*kWidthBase, _coverImageV.top, kScreenWidth - 100*kWidthBase, _coverImageV.height)];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:12];
    _contentLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_contentLabel];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_coverImageV.left, _contentLabel.bottom + 10*kHeightBase, _coverImageV.width * 2, 20*kHeightBase)];
    _timeLabel.font = [UIFont systemFontOfSize:10];
    [self.contentView addSubview:_timeLabel];
    
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_titleLabel.width - _timeLabel.width * 2 + 20*kWidthBase, _timeLabel.top, _timeLabel.width * 2, _timeLabel.height)];
    _commentLabel.font = [UIFont systemFontOfSize:10];
    _commentLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_commentLabel];
    
}




-(void)setModel:(PKCommunityPostModel *)model
{
    _model = model;
    
    _titleLabel.text = model.title;
    _contentLabel.text = model.content;
    _timeLabel.text = model.addtime_f;
    _commentLabel.text = [NSString stringWithFormat:@"评论数:%@",model.comment];
    
       if ([model.coverimg isEqualToString:@""]) {
   
       // 无图
       // 标示这个是没有图片的model
       _coverImageV.image = nil;
       // 修改cell的布局
       _contentLabel.frame = CGRectMake(_titleLabel.left, _titleLabel.bottom + 10*kHeightBase, _titleLabel.width, 50*kHeightBase);

       // 只有对没有图片的cell上的contentLabel自适应高度 :
       // 获取contentLabel的高度
       CGFloat contentHeight = [PKCommunityPostTableViewCell getHeightFromString:model.content];
       CGRect tempFrame = _contentLabel.frame;
       tempFrame.size.height = contentHeight;
       _contentLabel.frame = tempFrame;
       
       // 再修改两个小Label的frame
       _timeLabel.frame = CGRectMake(_coverImageV.left, _contentLabel.bottom + 10*kHeightBase, _coverImageV.width * 2, 20*kHeightBase);
        _commentLabel.frame = CGRectMake(_titleLabel.width - _timeLabel.width * 2 + 20*kWidthBase, _timeLabel.top, _timeLabel.width * 2, _timeLabel.height);
       // 再判断是不是精品
       if (model.ishot) {
           
           _hotImageV.image = [UIImage imageNamed:@"iconfont-iconfonthome"];
           
       } else
           _hotImageV.image = nil;
       
           
    }
    
    if (![model.coverimg isEqualToString:@""])
    {
        // 有图:
        [_coverImageV sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
        // 还原cell的布局
        _contentLabel.frame = CGRectMake(_coverImageV.right + 10*kWidthBase, _coverImageV.top, _titleLabel.width - _coverImageV.width - 10*kWidthBase, _coverImageV.height);
        // 再修改两个小Label的frame
        _timeLabel.frame = CGRectMake(_coverImageV.left, _contentLabel.bottom + 10*kHeightBase, _coverImageV.width * 2, 20*kHeightBase);
        _commentLabel.frame = CGRectMake(_titleLabel.width - _timeLabel.width * 2 + 20*kWidthBase, _timeLabel.top, _timeLabel.width * 2, _timeLabel.height);

        if (model.ishot) {
            
            _hotImageV.image = [UIImage imageNamed:@"iconfont-iconfonthome"];
        } else
            _hotImageV.image = nil;
        
    }
    
   
    
}




+ (CGFloat)getHeightFromString:(NSString *)str

{
    // 1.获取字体大小的字典
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName, nil];
    
    // 2.获取文字所占的frame
    CGRect frame = [str boundingRectWithSize:CGSizeMake(kScreenWidth - 40*kWidthBase, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 3.返回label的高度
    return frame.size.height;
    
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
